package com.zoshine.zzg.controller;

import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.Settlement;
import com.zoshine.zzg.pojo.pojostatus.SettlementStatus;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.SettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/settlement",method = RequestMethod.POST)
public class SettlementController extends BaseController{
    private SettlementService settlementService;
    @Autowired
    public void setSettlementService(SettlementService settlementService) {
        this.settlementService = settlementService;
    }

    /**
     * 新增供应商结算
     * @param idStr;
     */
    @RequestMapping("/addSettlement")
    public BaseResponse addSettlement(String idStr){
        settlementService.addSettlement(idStr);
        return AppResponse.OK;
    }

    /**
     * 获取供应商结算集合(分页)
     * @param settlement;
     * @return listSupplierSettlement
     */
    @RequestMapping("/pageSettlement")
    public BaseResponse pageSettlement(Settlement settlement){
        List<Long> supplierPower = super.getSupplierPower();
        List<Long> prisonPower = super.getPrisonPower();
        settlement.setSupplierPower(supplierPower);
        settlement.setPrisonPower(prisonPower);
        List<Settlement> listSettlement = settlementService.list(settlement);
        return new AppResponse(new PageInfo<>(listSettlement));
    }

    /**
     * 提交审核
     * @param idStr 结算单ID集合
     */
    @RequestMapping("submitAnAudit")
    public BaseResponse submitAnAudit(String idStr){
        changeStatus(idStr,SettlementStatus.PENDING_RECEIVE.getValue(),
          ErrorMessage.STATUS_SHOULD_BE_PENDING_RECEIVE,SettlementStatus.AUDITING.getValue());
        return AppResponse.OK;
    }
    /**
     * 审核通过
     * @param idStr 结算单ID集合
     */
    @RequestMapping("failureAudit")
    public BaseResponse failureAudit(String idStr){
        changeStatus(idStr,SettlementStatus.AUDITING.getValue(),
          ErrorMessage.STATUS_SHOULD_BE_AUDITING,SettlementStatus.AUDITED.getValue());
        return AppResponse.OK;
    }
    /**
     * 审核不通过
     * @param idStr 结算单ID集合
     */
    @RequestMapping("passAudit")
    public BaseResponse passAudit(String idStr){
        changeStatus(idStr,SettlementStatus.AUDITING.getValue(),
          ErrorMessage.STATUS_SHOULD_BE_AUDITING,SettlementStatus.AUDITED_FAILURE.getValue());
        return AppResponse.OK;
    }
    /**
     * 申请发票
     * @param idStr 结算单ID集合
     */
    @RequestMapping("applyForInvoice")
    public BaseResponse applyForInvoice(String idStr){
        changeStatus(idStr,SettlementStatus.AUDITED.getValue(),
          ErrorMessage.STATUS_SHOULD_BE_AUDITED,SettlementStatus.INVOICE_DELIVER.getValue());
        return AppResponse.OK;
    }
    /**
     * 寄送发票
     * @param idStr 结算单ID集合
     */
    @RequestMapping("sendInvoice")
    public BaseResponse sendInvoice(String idStr){
        changeStatus(idStr,SettlementStatus.INVOICE_DELIVER.getValue(),
          ErrorMessage.STATUS_SHOULD_BE_APPLY_FOR_INVOICE,SettlementStatus.INVOICE.getValue());
        return AppResponse.OK;
    }
    /**
     * 收取发票
     * @param idStr 结算单ID集合
     */
    @RequestMapping("receiveInvoice")
    public BaseResponse receiveInvoice(String idStr){
        changeStatus(idStr,SettlementStatus.INVOICE.getValue(),
          ErrorMessage.STATUS_SHOULD_BE_SEND_INVOICE,SettlementStatus.PRESENTING_BANK.getValue());
        return AppResponse.OK;
    }
    /**
     * 封装的方法
     * @param idStr 结算单ID集合
     * @param errorMessage 异常状态
     * @param beforeStatus 修改之前的状态
     * @param afterStatus 修改之后的状态
     */
    private void changeStatus(String idStr,Integer beforeStatus,ErrorMessage errorMessage,Integer afterStatus){
        //为空判断
        if (idStr == null || idStr.length()==0) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }
        String[] pidArr = idStr.split(",");
        List<Long> ids=new ArrayList<>();
        for (String id : pidArr) {
            //判断是否是待提交审核状态
            Long settlementId=Long.parseLong(id);
            Settlement settlement = settlementService.get(settlementId);
            if(!(settlement.getStatus().equals(beforeStatus))){
                throw new AppException(errorMessage);
            }
            ids.add(Long.parseLong(id));
        }
        if(ids.size()>0){
            settlementService.changeSettlementStatus(ids,afterStatus);
        }
    }
}
